/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.front.entrepot;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.services.front.entrepot.EntrepotService;
import shop.services.front.entrepot.bean.Entrepot;
import shop.services.front.spec.SpecService;
import shop.services.front.spec.bean.Spec;
import shop.services.manage.system.bean.User;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @类名称：EntrepotController
 * @创建人：Wzl
 * @创建时间：2017-10-24 上午09:59:13
 * @版本号：1.0
 * @描述：分销仓库
 */
@Controller
@RequestMapping("/front/entrepot/")
public class EntrepotController extends FrontBaseController<Entrepot> {
    @Resource
    private EntrepotService entrepotService;
    @Resource
    private SpecService specService;
    @Override
    public Services<Entrepot> getService() {
        return entrepotService;
    }

    /**
     * 跳转到我的仓库
     *
     * @return
     */
    @RequestMapping("toMyDepot")
    public String toMyDepot() {
        return "/front/depot/myDepot";
    }

    /**
     * 跳转到我的仓库订单详情页面
     *
     * @return
     */
    @RequestMapping("toMyDepotOrder")
    public String toMyDepotOrder() {
        return "/front/depot/myDepotOrder";
    }

    /**
     * 跳转到我的仓库填写发货信息页面
     *
     * @return
     */
    @RequestMapping("toAddLogistics")
    public String toAddLogistics() {
        return "/front/depot/addShipInf";
    }

    /**
     * 查询分销仓库
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("selectEntrepotList")
    @ResponseBody
    public JSONResult selectEntrepotList(HttpSession session,Entrepot entrepot) throws Exception {
        User user = (User) session.getAttribute("userInfo");
        entrepot.setUserID(user.getId());
        List<Entrepot> rsList = entrepotService.selectEntrepotList(entrepot);
        jsonResult = new JSONResult();
        jsonResult.setData(rsList);
        return jsonResult;
    }

    /**
     * 会员购买商品查询上级库存
     * @param entrepot
     * @return
     */
    @RequestMapping(value = "selectEntrepotDetail")
    @ResponseBody
    public JSONResult selectEntrepotDetail(Entrepot entrepot) {
        Spec spec =new Spec();
        spec.setProductID(entrepot.getProductID());
        spec.setSpecCombination(entrepot.getSpecInfo());
        spec=specService.selectOne(spec);
        entrepot =  entrepotService.selectOne(entrepot);
        if(entrepot!=null){
            entrepot.setSpecNowPrice(spec.getSpecNowPrice());
            entrepot.setSpecID(spec.getId());
        }else{
            entrepot=new Entrepot();
            entrepot.setSpecNowPrice(spec.getSpecNowPrice());
            entrepot.setSpecID(spec.getId());
            entrepot.setNumber(0);
        }
        jsonResult = new JSONResult();
        jsonResult.setData(entrepot);
        return jsonResult;
    }

    /**
     * 查询上级默认库存
     * @param entrepot
     * @return
     */
    @RequestMapping(value = "selectDefaultSpec")
    @ResponseBody
    public JSONResult selectDefaultSpec(Entrepot entrepot) {
        List<Entrepot> entrepotList=entrepotService.selectList(entrepot);
        int number=0;
        if(entrepotList!=null && entrepotList.size() !=0 )
            number=entrepotList.get(0).getNumber();
        jsonResult = new JSONResult();
        jsonResult.setData(number);
        return jsonResult;
    }

}
